package com.xc.content.controller;

import com.google.common.base.Preconditions;
import com.xc.content.bo.TeachPlanBO;
import com.xc.content.constant.ExceptionCode;
import com.xc.content.convert.TeachPlanConverter;
import com.xc.content.convert.TeachPlanServiceConverter;
import com.xc.content.dto.TeachPlanDTO;
import com.xc.content.service.TeachPlanService;
import com.xc.content.utils.ResponseResult;
import com.xc.content.utils.TeachPlanTreeNode;
import com.xc.content.utils.TreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-01-07
 * @Version: 1.0
 */
@RestController
@RequestMapping("/content/teachPlan")
@Api(value = "课程计划接口", tags = "课程计划接口")
@Slf4j
public class TeachPlanController {

    @Resource
    private TeachPlanService teachPlanService;

    @PostMapping("/addTeachPlan")
    @ApiOperation(value = "新增课程计划接口")
    public ResponseResult<?> addTeachPlan(@RequestBody List<TeachPlanDTO> teachPlanDTOList) {
        if (CollectionUtils.isEmpty(teachPlanDTOList)) {
            log.error("teachPlanDTOList为空！");
            throw new RuntimeException("teachPlanDTOList为空!");
        }
        List<TeachPlanBO> teachPlanBOList = TeachPlanConverter.INSTANCE.convertDTOListToBOList(teachPlanDTOList);
        Boolean isAdd = teachPlanService.addTeachPlan(teachPlanBOList);
        return ResponseResult.success("新增课程计划成功", isAdd);
    }

    @ApiOperation(value = "树型查询分类接口")
    @GetMapping("/getTeachPlanTree")
    public ResponseResult<?> getTeachPlanTree() {
        List<TeachPlanTreeNode> teachPlanTree = teachPlanService.getTeachPlanTree();
        if (CollectionUtils.isEmpty(teachPlanTree)){
            return ResponseResult.error(ExceptionCode.FIVE_HUNDRED_CODE,"查询失败！");
        }
        return ResponseResult.success("查询成功!",teachPlanTree);
    }

    @PostMapping("/updateTeachPlan")
    @ApiOperation(value = "修改课程计划接口")
    public ResponseResult<?> updateTeachPlan(@RequestBody TeachPlanDTO teachPlanDTO) {
        Preconditions.checkNotNull(teachPlanDTO.getId(), "课程计划id不能为空");

        TeachPlanBO teachPlanBO = TeachPlanConverter.INSTANCE.convertDTOToBO(teachPlanDTO);
        Boolean isUpdate = teachPlanService.updateTeachPlan(teachPlanBO);
        return ResponseResult.success("修改课程计划成功", isUpdate);

    }

    @PostMapping("/deletedTeachPlan")
    @ApiOperation(value = "删除课程计划接口")
    public ResponseResult<?> deletedTeachPlan(@RequestBody List<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            log.error("ids为空！");
            throw new RuntimeException("ids为空!");
        }
        Boolean isDeleted = teachPlanService.deletedTeachPlan(ids);
        return ResponseResult.success("修改课程计划成功", isDeleted);
    }







}
